package com.douban.app.control;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;

import com.alibaba.fastjson.JSONObject;

import com.douban.app.model.*;
import com.douban.app.param.SongQueryParam;
import com.douban.app.service.*;
import com.douban.app.util.SubjectUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;



import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;



import java.util.List;

@RestController
public class MainControl {
    @Autowired
    private SingerService singerService;
    @Autowired
    private SongService songService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private UserService userService;
    @Autowired
    private LikeService likeService;


    @GetMapping("/getone")
    public List<PlayModel> getOne() {
        ArrayList<PlayModel> playModels = new ArrayList<>();

        SongQueryParam songQueryParam = new SongQueryParam();
        int count = (int) (Math.random()*150);
        songQueryParam.setPageNum(count);
        count++;
        songQueryParam.setPageSize(10);
        Page<Song> list = songService.list(songQueryParam);
        for (Song song : list) {
            PlayModel playModel = new PlayModel();
            List<String> singerIds = song.getSingerIds();
            ArrayList<Singer> singers = new ArrayList<>();
            for (String id : singerIds) {
                singers.add(singerService.get(id));
            }
            PlayModel playModel1 = playModel.setAll(song, singers);
            playModels.add(playModel1);
        }
        return playModels;
    }

    @GetMapping("/index")
    public List<MhzModel> index(){
        MhzModel mhzModel1 = new MhzModel();
        MhzModel mhzModel2 = new MhzModel();
        MhzModel mhzModel3 = new MhzModel();
        List<MhzModel> list= new ArrayList<>();

        mhzModel1.setTitle("心情/场景");
        List<Subject> subjects1 = subjectService.getSubjects(SubjectUtil.TYPE_MHZ, SubjectUtil.TYPE_SUB_MOOD);
        mhzModel1.setSubjects(subjects1);


        mhzModel2.setTitle("语言/年代");
        List<Subject> subjects2 = subjectService.getSubjects(SubjectUtil.TYPE_MHZ, SubjectUtil.TYPE_SUB_AGE);
        mhzModel2.setSubjects(subjects2);

        mhzModel3.setTitle("风格/流派");
        List<Subject> subjects3 = subjectService.getSubjects(SubjectUtil.TYPE_MHZ, SubjectUtil.TYPE_SUB_STYLE);
        mhzModel3.setSubjects(subjects3);

        list.add(mhzModel1);
        list.add(mhzModel2);
        list.add(mhzModel3);

        return list;


    }

    @GetMapping("/beforeindex")
    public MhzModel beforeIndex(){
        MhzModel mhzModel = new MhzModel();
        mhzModel.setTitle("从艺术家出发");
        List<Subject> subjects = subjectService.getSubjects(SubjectUtil.TYPE_MHZ, SubjectUtil.TYPE_SUB_ARTIST);
        mhzModel.setSubjects(subjects);
        return mhzModel;
    }

    @GetMapping("/getartist/{id}")
    public List<PlayModel> getArtist(@PathVariable("id") String id){

        List<PlayModel> list = new ArrayList<>();

        if (!StringUtils.hasText(id)){
            return null;
        }
        Singer singer = singerService.get(id);
        List<Song> songs = singerService.songs(id);
        int count =0;
        for (Song song : songs) {
            PlayModel playModel = new PlayModel();
            PlayModel playModel1 = playModel.setOne(song, singer);
            list.add(playModel1);
            count++;
            if (count>20){
                break;
            }
        }
        return list;

    }

    @GetMapping("/collection")
    public List<Subject> getCollection(){
        List<Subject> subjects = subjectService.getSubjects(SubjectUtil.TYPE_COLLECTION);
        return subjects;
    }

    @GetMapping("/getrandom")
    public List<PlayModel> random(){
        ArrayList<PlayModel> playModels = new ArrayList<>();

        SongQueryParam songQueryParam = new SongQueryParam();
        int count = (int) (Math.random()*150);
        songQueryParam.setPageNum(count);
        count++;
        songQueryParam.setPageSize(8);
        Page<Song> list = songService.list(songQueryParam);
        for (Song song : list) {
            PlayModel playModel = new PlayModel();
            List<String> singerIds = song.getSingerIds();
            ArrayList<Singer> singers = new ArrayList<>();
            for (String id : singerIds) {
                singers.add(singerService.get(id));
            }
            PlayModel playModel1 = playModel.setAll(song, singers);
            playModels.add(playModel1);
        }
        return playModels;
    }

    @GetMapping("/search/{value}")
    public List<PlayModel> search(@PathVariable("value") String valu) throws UnsupportedEncodingException {
        String value = URLDecoder.decode(valu, "UTF-8");
        if (!StringUtils.hasText(value)){
            return null;
        }

        Singer singerBySingerName = singerService.getSingerBySingerName(value);
        List<Song> song1 = singerService.getSongBySingerName(value);
        List<Song> songByName = songService.getSongByName(value);
        System.out.println(songByName);

        if (song1!=null){
            ArrayList<PlayModel> playModels = new ArrayList<>();
            for (Song song : song1) {
                PlayModel playModel = new PlayModel();
                PlayModel playModel1 = playModel.setOne(song, singerBySingerName);
                playModels.add(playModel1);
            }
            return playModels;
        }
        if (songByName!=null){
            List<PlayModel> objects = new ArrayList<>();
            for (Song song : songByName) {
                PlayModel playModel = new PlayModel();
                Singer singer = singerService.get(song.getSingerIds().get(0));


                PlayModel playModel1 = playModel.setOne(song, singer);

                objects.add(playModel1);
            }
            return objects;
        }

        return null;
    }

    @GetMapping("/register/{num}/{pass}")
    public Dbuser registerUser(@PathVariable("num") String num, @PathVariable("pass") String pass){
        return userService.register(num,pass);
    }

    @GetMapping("/login/{num}/{pass}")
    public Dbuser loginUser(@PathVariable("num") String num, @PathVariable("pass") String pass ){
        return userService.login(num,pass);
    }

    @GetMapping("/update")
    public Dbuser updateUser(@RequestParam("id")String id, @RequestParam("url")String url, @RequestParam("name")String name, @RequestParam("pass")String pass) throws UnsupportedEncodingException {
        String decode = URLDecoder.decode(url, "UTF-8");
        return userService.update(id,decode,name,pass);
    }

    @PostMapping(value = "/add/like/{id}", consumes="application/json")
    public Ilike add(@RequestBody JSONObject jsonObject, @PathVariable("id") String id){
        return likeService.add(jsonObject,id);
    }

    @GetMapping("/get/likes/{id}")
    public List<Ilike> getLikes(@PathVariable("id") String id){
        List<Ilike> ilikes = likeService.allLike(id);
        if (ilikes==null){
            return null;
        }
        return ilikes;
    }




}
